package com.example.demo.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class PropertiesUtil {
    private static Properties properties = new Properties();

    public static Map read(String path) {
        try {
            InputStream inputStream = null;
            inputStream = new BufferedInputStream(new FileInputStream(path));
            properties.load(inputStream);
            Map<String, String> map = new HashMap<>();
            Set<Object> keySet = properties.keySet();
            for (Object object : keySet) {
                String key = (String) object;
                String value = properties.getProperty(key);
                map.put(key, value);
            }
            inputStream.close();
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void write(String path) {
        try {
            InputStream inputStream = new BufferedInputStream(new FileInputStream("D:\\Java\\idea\\demo\\src\\main\\resources\\a.properties"));
            properties.load(inputStream);
            properties.setProperty("phone", "123456");
            FileOutputStream fileOutputStream = new FileOutputStream(path);
            properties.store(fileOutputStream, "new properties");
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
